package com.jt.provider.features;

import java.util.LinkedHashMap;
import java.util.Map;

public class LruCache<k,v> extends LinkedHashMap<k,v> {
    LinkedHashMap<k, v> removeElements =
            new LinkedHashMap<k, v>();
    private int maxCap;

    public LruCache(int cap) {
        super((int) Math.ceil(cap / 0.75) + 1, 0.75f, true);
        this.maxCap = cap;
    }

    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<k, v> eldest) {
        boolean flag = size() > maxCap;
        if (flag) {
            removeElements.put(eldest.getKey(), eldest.getValue());
        }
        return flag;
    }

        public static void main(String[] args) {
            LruCache<String, Object> cache =
                    new LruCache<>(3);
            cache.put("A", 100);
            cache.put("B", 200);
            cache.put("C", 300);
            cache.get("A");
            cache.put("D", 400);
            cache.put("E", 500);
            System.out.println(cache);
            System.out.println(cache.removeElements);
        }
    }
